//connect to database
function connectDatabase() {
	var db = window.openDatabase("minnanonihongo", "1.0",
			"Elementary Japanese Database", 500000);
	db.transaction(populateDB, errorCB, successCB);
}

// tao cac bang trong db
function populateDB(tx) {
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS dataversionid (dt_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, dt_version INTEGER)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS vocabulary (v_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, v_word       TEXT, v_hiragana        TEXT, v_mean_kanji 	TEXT, v_mean_en 	TEXT, v_mean_vi 	TEXT, v_lesson_id 		INTEGER, v_note 			TEXT, v_dataversion_id INTEGER)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS testtype   (t_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, t_type       TEXT, r_dataversion_id  TEXT)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS reading    (r_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, r_title      TEXT, r_content_jp      TEXT, r_content_en 	TEXT, r_content_vi 	TEXT, r_lesson_id 	TEXT, r_dataversion_id 	TEXT)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS question   (q_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, q_question   TEXT, q_anwser1         TEXT, q_anwser2 		TEXT, q_anwser3 	TEXT, q_anwser4 	TEXT, q_correct 		TEXT, 	 q_type 			TEXT, q_dataversion_id TEXT)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS listening  (li_id INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, li_lesson_id TEXT, li_title          TEXT, li_thumb 		TEXT, li_script_jp 	TEXT, li_script_en 	TEXT, li_script_vi 	TEXT, li_track 			TEXT, 	 li_dataversion_id 	TEXT)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS lesson     (le_id INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, le_title     TEXT, le_dataversion_id TEXT)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS kanji      (k_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, k_kanji      TEXT, k_mean_kanji      TEXT, k_mean_en 		TEXT, k_mean_vi 	TEXT, k_onyomi 		TEXT, k_kunyomi 		TEXT, 	 k_remember 		TEXT, k_on_ex 		   TEXT,k_kun_ex 		 TEXT, k_dataversion_id TEXT)');
	tx
			.executeSql('CREATE TABLE IF NOT EXISTS grammar    (g_id  INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT, g_title      TEXT, g_mean            TEXT, g_explain 		TEXT, g_note 		TEXT, g_example_jp 	TEXT, g_example_en 		TEXT, 	 g_example_vi 		TEXT, g_lesson_id 	   TEXT,g_dataversion_id TEXT)');
}

function errorCB(err) {
	console.log("Error processing SQL:" + err.code);
	document.getElementById('sql-result').innerHTML = "<STRONG>Error processing SQL: "
			+ err.code + "</STRONG>";
}

function successCreateCB() {
	console.log("Database has been created successfully");
	document.getElementById('sql-result').innerHTML = "<STRONG>DATABASE HAS BEEN CREATED SUCCESSFULLY</STRONG>";
}

function querySuccess(tx, results) {
	console.log("Rows Effected = " + results.rowEffected);
	console.log("No of Rows = " + results.rows.length);
	document.getElementById('sql-result').innerHTML = ("<STRONG>number of rows"
			+ results.rows.length + "</STRONG>");
}

function queryDB(tx) {
	tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

function getSqlResultSet() {
	if (!db) {
		db = window.openDatabase("minnanonihongo", "1.0", "japanese database",
				500000);
	}
	db.transaction(queryDB, errorCB);
}

function writeLocalStorage() {
	window.localStorage.setItem("myKey", "vmSoftTech");
	var keyname = window.localStorage.key(0);
	document.getElementById('local-storage-result').innerHTML = "Wrote key: <strong>"
			+ keyname + "</strong>";
}

// As phonegap supports localstorage we can write values to local storage using
// the above method.
function readLocalStorage() {
	var value = window.localStorage.getItem("myKey");
	if (!value) {
		document.getElementById('local-storage-result').innerHTML = "<strong>Null</strong> - Try Write first";
	} else {
		document.getElementById('local-storage-result').innerHTML = "Got value: <strong>"
				+ value + "</strong>";
	}
}

// As phonegap supports localstorage we can read values from localstorage using
// the above method.
function removeItemLocalStorage() {
	window.localStorage.removeItem("myKey");
	document.getElementById('local-storage-result').innerHTML = "Removed key/value: <strong>myKey/vmSoftTech</strong>";
}
